#include <string.h>
#include <signal.h>
#include "minimal-ws-server-pmd.h"
#include "ws-server-cmd-sch.h"
#include "gr5513_ota_m.h"

static struct mymsg my_msg;
extern struct lws *my_wsi;
extern void ble_scan(void *arg);
extern void ble_adv(uint16_t overtime);

extern volatile int signal_received;

static void ws_rx_tx_sigint_handler(int sig)
{
	signal_received = sig;
}

void send_data_to_ws(char *buff ,uint16_t len ,uint8_t type)
{
	memset(my_msg.ws_buff, 0, sizeof(my_msg.ws_buff));
	my_msg.len = len;
	my_msg.type = type;
	lws_snprintf(my_msg.ws_buff, my_msg.len ,"%s\n" , buff);
	printf("\033[1;33m send_data_to_ws len = %d %s \033[0m\n",len,my_msg.ws_buff);
}
void read_data_from_ws(char *buff ,uint16_t len)
{
	memset(my_msg.ws_receivebuff, 0, sizeof(my_msg.ws_receivebuff));
	memcpy(my_msg.ws_receivebuff, buff, len);
	my_msg.r_len = len;
	printf("\033[1;33m ws_receivebuff len = %d %s \033[0m\n",len,my_msg.ws_receivebuff);
}

//线程接收ws数据解码
void *ws_send_cmd_data(void *arg)
{
	struct per_vhost_data__minimal *vhd =
			(struct per_vhost_data__minimal *)arg;
	struct msg amsg;
	int n;

	signal(SIGINT, ws_rx_tx_sigint_handler);

	do {
		/* don't generate output if nobody connected */
		if (signal_received == SIGINT)
		{
			printf("ws_send_cmd_data signal_received %d \n", signal_received);
			goto exit;
		}
		if (!vhd->pss_list)
			goto wait;
		if(my_msg.len <=0 )
			goto wait;
		pthread_mutex_lock(&vhd->lock_ring); /* --------- ring lock { */

		/* only create if space in ringbuffer */
		n = (int)lws_ring_get_count_free_elements(vhd->ring);
		if (!n) {
			lwsl_user("dropping!\n");
			goto wait_unlock;
		}

		amsg.payload = malloc((unsigned int)(LWS_PRE + (my_msg.len+20)));
		if (!amsg.payload) {
			lwsl_user("OOM: dropping\n");
			goto wait_unlock;
		}

		switch (my_msg.type)
		{
		case 0: // ok
			n = lws_snprintf((char *)amsg.payload + LWS_PRE, (unsigned int)(my_msg.len+20),
							 "%s:%s\n", "ok_", my_msg.ws_buff);
			break;
		case 1: //err
			n = lws_snprintf((char *)amsg.payload + LWS_PRE, (unsigned int)(my_msg.len+20),
							 "%s:%s\n", "err_", my_msg.ws_buff);
			break;
		case 2: //sys
			n = lws_snprintf((char *)amsg.payload + LWS_PRE, (my_msg.len+20),
							 "%s:%s\n", vhd->config, my_msg.ws_buff);
			break;
		case 3: //q
			n = lws_snprintf((char *)amsg.payload + LWS_PRE, (my_msg.len+20),
							 "%s:%s\n", vhd->config, my_msg.ws_buff);
			goto exit;
			break;
		case 4: //q
			n = lws_snprintf((char *)amsg.payload + LWS_PRE, (my_msg.len+20),
							 "%s:%s\n", "Fs", my_msg.ws_buff);
			break;
		default:
			break;
		}
		my_msg.len = 0;
		printf("\033[1;32m mws_send_cmd_data len = %d %s \033[0m\n",strlen((char *)amsg.payload + LWS_PRE),((unsigned char *)amsg.payload) + LWS_PRE);
		//n = lws_write(my_wsi, ((unsigned char *)amsg.payload) + LWS_PRE,strlen((char *)amsg.payload + LWS_PRE), LWS_WRITE_TEXT);
		amsg.len = (unsigned int)n;
 		n = (int)lws_ring_insert(vhd->ring, &amsg, 1);
		if (n != 1) {
			__minimal_destroy_message(&amsg);
			lwsl_user("dropping!\n");
		} else
			 lws_cancel_service(vhd->context);

wait_unlock:
		pthread_mutex_unlock(&vhd->lock_ring); /* } ring lock ------- */

wait:
		usleep(5000);

	} while (!vhd->finished);
exit:
	printf("ws_send_cmd_data exiting\n");

	pthread_exit(NULL);

	return NULL;
}

//线程发送数据到ws
void *ws_parse_receive_cmd(void *arg)
{
	struct per_vhost_data__minimal *vhd =
			(struct per_vhost_data__minimal *)arg;
	static uint32_t count=0;
	static char test[20];
	
	signal(SIGINT, ws_rx_tx_sigint_handler);
	printids(__func__);

	do
	{
		if (signal_received == SIGINT)
		{
			printf("ws_parse_receive_cmd signal_received %d \n", signal_received);
			goto done;
		}
		if(my_msg.r_len <=0 )
			goto wait;
		if(strcmp(my_msg.ws_receivebuff,"bleota\n") == 0)
		{
			app_ota_start();
			my_msg.r_len = 0;
			memset(my_msg.ws_receivebuff, 0, sizeof(my_msg.ws_receivebuff));
			goto cancel_r;
		}
		if(strcmp(my_msg.ws_receivebuff,"blescan\n") == 0)
		{
			ble_scan(NULL);
			my_msg.r_len = 0;
			memset(my_msg.ws_receivebuff, 0, sizeof(my_msg.ws_receivebuff));
			goto cancel_r;
		}
		if(strcmp(my_msg.ws_receivebuff,"bleadv:"))
		{
			uint16_t overtime_temp = 0;
			char *pCh = strstr(my_msg.ws_receivebuff,"bleadv:");
			if(sscanf(pCh,"%*[^:]:%ds%[^\r\n]",&overtime_temp))
			{
				printf("adv over time is %d s\n",overtime_temp);
				ble_adv(overtime_temp);
				my_msg.r_len = 0;
				memset(my_msg.ws_receivebuff, 0, sizeof(my_msg.ws_receivebuff));
			}
			else{
				printf("bleadv over time err\n");
			}
			goto cancel_r;
		}
cancel_r:		
		lws_cancel_service(vhd->context);
wait:
		usleep(5000);
	} while (!vhd->finished);

done:
	printf("ws_parse_receive_cmd exiting\n");
	pthread_exit(NULL);

	return NULL;
}